#include <Hello_Triangle.h>

Hello_Triangle::Hello_Triangle(/* args */)
{

    // 顶点着色器
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    // 片段着色器
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    const GLchar *fragmentShaderSource = getFragmentShaderSource(1.0f, 1.0f, 0.0f).c_str();
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    // 着色器程序
    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);



    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO); // 生成索引
    
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0); // 参数是顶点属性的索引 location
}

Hello_Triangle::~Hello_Triangle()
{
}

void Hello_Triangle::draw()
{
    glUseProgram(shaderProgram);
    glBindVertexArray(VAO);
    // glDrawArrays(GL_TRIANGLES, 0, 3);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

/// @brief 获取片段着色器
/// @param r
/// @param g
/// @param b
/// @param a
/// @return
std::string getFragmentShaderSource(float r, float g, float b, float a /*=1.0*/)
{
    std::string res;
    res += "#version 330 core\n";
    res += "out vec4 FragColor;\n";
    res += "void main()\n";
    res += "{\n";
    res += "   FragColor = vec4(" + std::to_string(r) + ", " + std::to_string(g) + ", " + std::to_string(b) + ", " + std::to_string(a) + ");\n";
    res += "}\n";
    return res;
}



Hello_Triangles::Hello_Triangles(/* args */)
{

    // 顶点着色器
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    // 片段着色器
    fragmentShaderY = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar *fragmentShaderSourceY = getFragmentShaderSource(1.0f, 1.0f, 0.0f).c_str();
    glShaderSource(fragmentShaderY, 1, &fragmentShaderSourceY, NULL);
    glCompileShader(fragmentShaderY);

    fragmentShaderR = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar *fragmentShaderSourceR = getFragmentShaderSource(1.0f, 0.0f, 0.0f).c_str();
    glShaderSource(fragmentShaderR, 1, &fragmentShaderSourceR, NULL);
    glCompileShader(fragmentShaderR);



    // 着色器程序
    shaderProgramY = glCreateProgram();
    glAttachShader(shaderProgramY, vertexShader);
    glAttachShader(shaderProgramY, fragmentShaderY);
    glLinkProgram(shaderProgramY);
    glDeleteShader(fragmentShaderY);

    shaderProgramR = glCreateProgram();
    glAttachShader(shaderProgramR, vertexShader);
    glAttachShader(shaderProgramR, fragmentShaderR);
    glLinkProgram(shaderProgramR);
    glDeleteShader(fragmentShaderR);
    glDeleteShader(vertexShader);

    glGenVertexArrays(2,VAO);
    glGenBuffers(2, VBO);

    glBindVertexArray(VAO[0]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(firstTriangle), firstTriangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);



    glBindVertexArray(VAO[1]);
    glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(secondTriangle), secondTriangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0); // 参数是顶点属性的索引 location
}

Hello_Triangles::~Hello_Triangles()
{



}

void Hello_Triangles::draw()
{
    glUseProgram(shaderProgramY);
    glBindVertexArray(VAO[0]);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    // glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    glUseProgram(shaderProgramR);
    glBindVertexArray(VAO[1]);
    glDrawArrays(GL_TRIANGLES, 0, 3);
     // glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}



